package service.impl;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import constants.SystemConstants;
import entity.ResponseResult;
import entity.SgArticle;
import mapper.SgArticleMapper;
import org.springframework.stereotype.Service;
import service.SgArticleService;
import utils.BeanCopyUtils;
import vo.HotArticleVo;

import java.util.List;

/**
 * 文章表(SgArticle)表服务实现类
 *
 * @author makejava
 * @since 2022-04-08 14:54:58
 */
@Service("sgArticleService")
public class SgArticleServiceImpl extends ServiceImpl<SgArticleMapper, SgArticle> implements SgArticleService {

    @Override
    public ResponseResult hotArticleList() {
        //查询热门文章 封装成ResponseResult返回
        LambdaQueryWrapper<SgArticle> queryWrapper = new LambdaQueryWrapper<>();
        //必须是正式文章
        queryWrapper.eq(SgArticle::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        //按照浏览量进行排序
        queryWrapper.orderByDesc(SgArticle::getViewCount);
        //最多只查询10条
        Page<SgArticle> page = new Page<>(1,10);
        page(page, queryWrapper);
        List<SgArticle> records = page.getRecords();
        //bean拷贝
        List<HotArticleVo> articleVos = BeanCopyUtils.copyBeanList(records, HotArticleVo.class);
        return ResponseResult.okResult(articleVos);
    }
}

